package com.example.deliveryapplication

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.deliveryapplication.entity.CU
import com.example.deliveryapplication.entity.Client
import com.example.deliveryapplication.entity.SelectString
import com.example.deliveryapplication.sql.BearingModelDao
import com.example.deliveryapplication.sql.CUDao
import com.example.deliveryapplication.sql.ClientDao
import com.example.deliveryapplication.sql.PlanItemDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CUSetViewModel:BaseViewModel() {
    private val mResult = MutableLiveData<List<CU>>()
    val result: LiveData<List<CU>> get() = mResult
    fun getAll(clientDao:CUDao){
        launch {
            val clients = clientDao.loadAllChart()
            mResult.postValue(clients)
        }
    }
    private val mDeleteResult = MutableLiveData<Int>()
    val deleteResult: LiveData<Int> get() = mDeleteResult
    fun deleteModel(planItemDao: PlanItemDao, clientDao: CUDao, clientId:Int){
        launch {
//            val list = planItemDao.loadCUById(clientId)
//            if(list.isEmpty()){
//                val bean=clientDao.loadClientById(clientId)
//                val result=clientDao.deleteClient(bean)
//                mDeleteResult.postValue(result)
//            }else{
//                mDeleteResult.postValue(-1)
//            }

        }
    }
    private val mUpdateResult = MutableLiveData<Int>()
    val updateResult: LiveData<Int> get() = mUpdateResult
    fun updateClient( clientDao: CUDao, client: CU){
        launch {
            val result=clientDao.updateCU(client)
            mUpdateResult.postValue(result)
        }
    }
}